package de.thm.softwareprojekt.kalender;

import java.awt.*;
import javax.swing.*;

import com.toedter.calendar.JDateChooser;

import de.thm.softwareprojekt.DatenbankManager;
import de.thm.softwareprojekt.SWTProjekt;

import java.awt.event.*;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


public class DatumDialog extends JDialog implements ActionListener, WindowListener{
	private JDateChooser dateC;
	private JTimeChooser timeC;
	private JTextArea notizFeld;
	private Termin termin = null;
	private Termin oTermin;
	private DatenbankManager dbengine;
	private Boolean modifyFlag = false;
	
	public DatumDialog(JFrame frame, Termin termin){
		super(frame,"Termin eintragen",true);
		oTermin = termin;
		Container cp = this.getContentPane();
		cp.setLayout(new BorderLayout());
		JPanel tempPanel = new JPanel();
		tempPanel.setLayout(new BorderLayout());
		dateC = new JDateChooser();
		timeC = new JTimeChooser();
		tempPanel.add("North", dateC);
		tempPanel.add("South", timeC);
		notizFeld = new JTextArea();
		notizFeld.setLineWrap(true);
		notizFeld.setFont(new Font("Arial", Font.PLAIN, 11));
		JButton speichern = new JButton("Speichern");
		cp.add("North", tempPanel);
		cp.add("Center",notizFeld);
		cp.add("South",speichern);
		speichern.setActionCommand("speichern");
		speichern.addActionListener(this);
		dbengine = ((SWTProjekt) frame).getGlobalmanager().getDBEngine();
		if (termin != null) {
			showProductInformation(termin);
			modifyFlag = true;
		}
		this.addWindowListener(this);
		this.setSize(300,150);
		this.setVisible(true);
	}
	
	public void showProductInformation(Termin termin){
		dateC.setDate(termin.getDatum());
		System.out.println(termin.getDatum().getMinutes());
		timeC.setTime(termin.getDatum());
		notizFeld.setText(termin.getNotiz());
	}

	@SuppressWarnings("deprecation")
	public void actionPerformed(ActionEvent event) {
		if (event.getActionCommand() == "speichern"){
			Calendar ca = dateC.getCalendar();
			Date date = new Date();
			date.setDate(ca.get(Calendar.DAY_OF_MONTH));
			date.setMonth(ca.get(Calendar.MONTH));
			date.setYear(ca.get(Calendar.YEAR));
			date.setHours(timeC.getTime().getHours());
			date.setMinutes(timeC.getTime().getMinutes());
			if (!modifyFlag)
				termin = new Termin(dbengine.getNextID("Termine"), date, notizFeld.getText());
			else{
				termin = new Termin(oTermin.getTerminnr(), date, notizFeld.getText());}
			this.setVisible(false);
		}
	}
	
	public Termin getTerminFromDialog(){
		return termin;
	}
	
	public boolean modifyMode(){
		return modifyFlag;
	}
	
	public void initDate(){
		dateC.setDate(new Date("20.12.1983"));
	}
	
	
	
	class JTimeChooser extends JSpinner {
		JSpinner.DateEditor editor;
	    public JTimeChooser() {
	        super(new SpinnerDateModel());
	        editor = new JSpinner.DateEditor(this, "HH:mm");
	    	setEditor(editor);
	    	editor.getTextField().setHorizontalAlignment(JFormattedTextField.LEFT);
	    }
	    
	    public Date getTime() {
	    	return (Date) this.getValue();
	    } 
	    
	    public void setTime(Date d){
	    	this.setValue(d);
	    	//getTextField().setText(time);
	    }
	}
	   
	@Override
	public void windowActivated(WindowEvent arg0) {	
	}

	@Override
	public void windowClosed(WindowEvent arg0) {		
	}

	public void windowClosing(WindowEvent e) {
	    e.getComponent().setVisible(false);  
	    termin = null;
	   }

	@Override
	public void windowDeactivated(WindowEvent arg0) {
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
	}
}
